package com.mdd.front.controller;


import com.mdd.common.aop.NotLogin;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.entity.ChinaCity;
import com.mdd.common.entity.ChinaProvince;
import com.mdd.front.service.CarScoreService;
import com.mdd.front.validate.article.QueryCarScoreRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/carScore")
@Api(tags = "跑分")
public class CarScoreController {

    @Resource
    CarScoreService carScoreService;

    @NotLogin
    @GetMapping("/queryAllChinaCity")
    @ApiOperation(value="查询所有省市区")
    AjaxResult<List<ChinaProvince>> queryAllChinaCity(){

        List<ChinaCity> allChinaProvince = carScoreService.queryAllChinaProvince();
        ArrayList<ChinaProvince> chinaProvinceList = new ArrayList<>();
        for (ChinaCity province : allChinaProvince) {
            ChinaProvince chinaProvince = new ChinaProvince();
            List<ChinaCity> chinaCities = carScoreService.queryAllCitiesByPid(province.getId());
            chinaProvince.setCityList(chinaCities);
            chinaProvince.setProvinceName(province.getCityName());
            chinaProvinceList.add(chinaProvince);
        }
        return AjaxResult.success(chinaProvinceList);
    }

    @PostMapping("queryCarScore")
    AjaxResult<BigDecimal> queryCarScore(@RequestBody QueryCarScoreRequest request){
        ChinaCity chinaCity = carScoreService.queryCityByCityName(request.getCityName());
        String cityLevel = chinaCity.getCityLevel();
        BigDecimal price=BigDecimal.ZERO;
        switch (cityLevel){
            case "1":
                price=BigDecimal.valueOf(45);
                break;
            case "2":
                price=BigDecimal.valueOf(37.5);
                break;
            case "3":
                price=BigDecimal.valueOf(32.5);
                break;
            default:
                price=BigDecimal.valueOf(25);

        }
        BigDecimal totalPrice = price.multiply(BigDecimal.valueOf(request.getWorkTimeEveryDay()))
                .multiply(BigDecimal.valueOf(request.getDurationOfDays())).setScale(2, RoundingMode.HALF_UP);
        return AjaxResult.success(totalPrice);
    }
}